package com.naizhuai.ui.user.simulation;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.network.exception.ValidateException;
import com.naizhuai.ui.entity.User.Simulation;
import com.naizhuai.ui.task.adapter.TaskExplainAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author：lichenxi
 * @date 2018/6/14 14
 * email：525603977@qq.com
 * Fighting
 */
public class WriteDataAdapter extends RecyclerView.Adapter<WriteDataAdapter.ViewHolder> {
    private List<Simulation> mSimulationList;
    private  boolean editenable=true;

   public void setEditenable(boolean editenable) {
        this.editenable = editenable;
    }

    public void setSimulation(List<Simulation> simulationList) {
        mSimulationList = simulationList;
        notifyDataSetChanged();
    }

    public List<Simulation> getSimulation() throws ValidateException {
        if (mSimulationList==null){
            throw new ValidateException("数据为空");
        }
        for (Simulation simulation : mSimulationList) {
            if (TextUtils.isEmpty(simulation.keyName)){
                throw new ValidateException(simulation.keyDetail+"尚未填写");
            }
        }
      return mSimulationList;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_simulation_data, parent, false);
        return new ViewHolder(view,new MyCustomEditTextListener());
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Simulation simulation=  mSimulationList.get(position);
        holder.title.setText(simulation.keyDetail);
        holder.detial.setEnabled(editenable);
        holder.detial.setText(simulation.value);
        holder.mEditTextListener.updatePosition(position);
    }

    @Override
    public int getItemCount() {
        return mSimulationList!=null?mSimulationList.size():0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.detial)
        EditText detial;
        MyCustomEditTextListener mEditTextListener;
        public ViewHolder(View itemView,MyCustomEditTextListener mEditTextListener) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mEditTextListener=mEditTextListener;
            detial.addTextChangedListener(mEditTextListener);
        }
    }
    private class MyCustomEditTextListener implements TextWatcher {
        private int position;

        public void updatePosition(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            // no op
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            mSimulationList.get(position).value = charSequence.toString();
        }
        @Override
        public void afterTextChanged(Editable editable) {
            // no op
        }
    }
}
